﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECommerce.Model.Product;
using Jzy.Core.Common;
using ECommerce.DAL.Product;
using Jzy.Core.Attach;
using System.IO;
using Jzy.Core.Dict;
using ECommerce.Common;

namespace ECommerce.BLL.Product
{
    public class ECProductBO : CommonOperate<ECProductInfo>
    {
        private ECProductDAL _product;
        public ECProductBO()
        {
            _product = new ECProductDAL();
        }

        public override ECProductInfo GetInfo(object id)
        {
            ECProductInfo info = base.GetInfo(id);
            if (id != null)
            {
                SetDelegate(info);
            }
            DictBO dict = new DictBO();
            IEnumerable<DictInfo> dictList = dict.GetList((int)DictItem.品牌分类).Where(T => T.Code == info.Type1);
            if (dictList.Count() > 0)
            {
                info.Type1Name = dictList.First().Name;
            }

            return info;
        }

        public override List<ECProductInfo> GetList(PagerSetting pager, params object[] conditionParas)
        {
            List<ECProductInfo> list = base.GetList(pager, conditionParas);
            DictBO dict = new DictBO();
            IEnumerable<DictInfo> dictList = dict.GetList((int)DictItem.品牌分类);
            foreach (ECProductInfo info in list)
            {
                IEnumerable<DictInfo> tmp = dictList.Where(T => T.Code == info.Type1);
                if (tmp.Count() > 0)
                {
                    info.Type1Name = tmp.First().Name;
                }
            }
            return list;
        }

        private void SetDelegate(ECProductInfo info)
        {
            ImageBO image = new ImageBO("ECProductPicture");
            info.GetPictures = productID => image.GetList(productID, null);
            info.GetComents = productID => (new ECProductCommentBO()).GetDisplayList(productID);

        }

        public void DeleteSmallImage(int id)
        {
            ECProductInfo info = this.GetInfo(id);
            if (!string.IsNullOrEmpty(info.SmallImage))
            {
                string phyPath = System.Web.HttpContext.Current.Server.MapPath(info.SmallImageAbsPath);
                if (File.Exists(phyPath))
                {
                    File.Delete(phyPath);
                }
                
            }
        }        

        public List<ECProductInfo> GetFavoriteList(int memberID)
        {
            List<ECProductInfo> list = _product.GetFavoriteList(memberID);
            return list;
        }

        public void DeleteAdImage(int id)
        {
            ECProductInfo info = this.GetInfo(id);
            if (!string.IsNullOrEmpty(info.AdImage))
            {
                string phyPath = System.Web.HttpContext.Current.Server.MapPath(info.AdImageAbsPath);
                if (File.Exists(phyPath))
                {
                    File.Delete(phyPath);
                }
            }
        }

        public string SaveImage(System.Web.HttpPostedFile file, int size)
        {
            UserImage userImage = new UserImage("ECProduct");
            string path = userImage.Save(file,size);
            return path;
        }

        public override void Delete(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                this.Delete(id);
            }
        }

        public override void Delete(object id)
        {
            ImageBO picture = new ImageBO("ECProductPicture");

            this.DeleteSmallImage((int)id);
            this.DeleteAdImage((int)id);

            picture.DeleteByBindID(id);

            base.Delete(id);

        }

        public void AddToFavorite(int memberID, int productID)
        {
            _product.AddToFavorite(memberID, productID);

        }

        

        /// <summary>
        /// 取出销量靠前的产品
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<ECProductInfo> GetTopList(int count)
        {
            return _product.GetTopList(count);
        }

        public void DeleteFavorite(int memberID, int productID)
        {
            _product.DeleteFavorite(memberID, productID);
        }

        public List<ECProductInfo> GetNewList(int count)
        {
            List<ECProductInfo> list = _product.GetNewList(count);
            return list;
        }

        public List<ECProductInfo> GetRecommendList(int count)
        {
            List<ECProductInfo> list = _product.GetRecommendList(count);
            return list;
        }
    }
}
